var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'attribute vec2 a_TexCoord;\n' +
  'varying vec2 v_TexCoord;\n' +
  'void main() {\n' +
  '  gl_Position = a_Position;\n' +
  '  v_TexCoord = a_TexCoord;\n' +
  '}\n';

// Fragment shader program
var FSHADER_SOURCE =
  '#ifdef GL_ES\n' +
  'precision mediump float;\n' +
  '#endif\n' +
  'uniform sampler2D u_Sampler;\n' +
  'uniform sampler2D u_Sampler1;\n' +
  'varying vec2 v_TexCoord;\n' +
  'void main() {\n' +
  '  vec4 c1 = texture2D(u_Sampler, v_TexCoord);\n' +
  '  vec4 c2 = texture2D(u_Sampler1, v_TexCoord);\n' +
  '  gl_FragColor = c1 * c2;\n' +
  '}\n';

var canvas, gl;
var verticesArr, verticesBuf, texture,texture2;

function initEnv() {
  canvas = document.getElementById('example');
  gl = getWebGLContext(canvas);
  // Specify the color for clearing <canvas>
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to intialize shaders.');
    return;
  }
}

function initVertices() {
  var n = 4;
  verticesArr = new Float32Array([
    // Vertex coordinate, Texture coordinate
    -1.0, 1.0, 0, 1,
    -1.0, -1.0, 0, 0,
    1.0, 1.0, 1, 1,
    1.0, -1.0, 1, 0
  ]);
  verticesBuf = gl.createBuffer();
  var FSIZE = verticesArr.BYTES_PER_ELEMENT;
  var a_Position = gl.getAttribLocation(gl.program, "a_Position");
  var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');

  if (!verticesBuf || a_Position < 0 || a_TexCoord < 0) {
    console.log(`initVertices error`);
    return -1;
  }

  gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuf);
  gl.bufferData(gl.ARRAY_BUFFER, verticesArr, gl.STATIC_DRAW);

  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0);
  gl.enableVertexAttribArray(a_Position);

  gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);
  gl.enableVertexAttribArray(a_TexCoord);

  // unbind
  gl.bindBuffer(gl.ARRAY_BUFFER, null);

  return n;
}

var textureLoadCount = 0;
function initTextures() {
  texture = gl.createTexture();
  texture2 = gl.createTexture();
  var u_Sampler = gl.getUniformLocation(gl.program, "u_Sampler");
  var u_Sampler1 = gl.getUniformLocation(gl.program, "u_Sampler1");
  var image = new Image();
  image.onload = function (e) {
    textureLoadCount++
    loadTexture(gl, 4, texture, u_Sampler, image,0);
  }
  var image2 = new Image();
  image2.onload = function (e) {
    textureLoadCount++
    loadTexture(gl, 4, texture2, u_Sampler1, image2,1);
  }
  image.src = "../webgl-cookbook-master/resources/sky.jpg";
  image2.src = "../webgl-cookbook-master/resources/circle.gif";

  if (!texture || !u_Sampler) {
    console.log(`initTextures error`);
    return false;
  }

  return true;
}

function loadTexture(gl, n, texture, u_Sampler, image,textureUnitNum) {
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
  gl.activeTexture(gl[`TEXTURE${textureUnitNum}`]);
  gl.bindTexture(gl.TEXTURE_2D, texture);

  // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
  gl.uniform1i(u_Sampler, textureUnitNum);

  if(textureLoadCount == 2){
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
  }
}

function init() {
  initEnv();
  initVertices();
  initTextures();
}

function main() {
  init();
}